package com.gensdai.leliang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.flyco.roundview.RoundTextView;
import com.gensdai.leliang.R;
import com.gensdai.leliang.base.BaseActivity;
import com.gensdai.leliang.common.bean.ApplyInfoBean;
import com.gensdai.leliang.entity.parseBean.BaseParse;
import com.gensdai.leliang.entity.parseBean.ProductInfo;
import com.gensdai.leliang.remoteInterface.RetrofitFactory;
import com.gensdai.leliang.retrofitUtils.BaseObserver;
import com.gensdai.leliang.retrofitUtils.ModelFilteredFactory;
import com.gensdai.leliang.utils.PreferencesUtils;
import com.gensdai.leliang.view.Toaster;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 申请退款的界面
 * Created by zsxx on 2017/9/20.
 */

public class ApplyRefund extends BaseActivity {

    public static final String EXTRA_ORDERID = "extraOrderId";

    @BindView(R.id.servicename)
    TextView servicename;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.groutbei)
    TextView groutbei;
    @BindView(R.id.amountMoney)
    TextView amountMoney;
    @BindView(R.id.outpayBtn)
    RoundTextView outpayBtn;

    private String orderid;

    KProgressHUD hud;
    @Override
    protected String getTitleStr() {
        return "申请退款";
    }

    @Override
    protected boolean isShowBackButton() {
        return true;
    }

    @Override
    protected boolean isCustomTitle() {
        return false;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_apply_refund;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        orderid = getIntent().getStringExtra(EXTRA_ORDERID);
        ButterKnife.bind(this);

        hud = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在处理...")
                .setCancellable(true)
                .setAnimationSpeed(1)
                .setDimAmount(0.5f);

        if(null != orderid)
            getApplyInfo(orderid);
    }

    private void getApplyInfo(String oid){

        String userid = PreferencesUtils.getStringPreference(this, OrderPreperActivity.prefreceName, OrderPreperActivity.prefrece_userid, ""); //获取用户id没有登陆就是没有

        Map<String, String> params = new HashMap<>();
        params.put("orderId", oid);
        params.put("userid", userid);
        ModelFilteredFactory.compose(RetrofitFactory.getInstance().API().getApplyInfo(params))
                .compose(this.<BaseParse<ApplyInfoBean>>bindUntilEvent(ActivityEvent.DESTROY)) //跟生命周期绑定一起
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        //开始监听前的处理，一般用于显示dialog什么的
////                        if (hud != null) {
////                            hud.show();
////                        }
                        if (hud != null && !hud.isShowing()) {
                            hud.show();
                        }
                        Log.d("accept", "accept");
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ApplyInfoBean>(this) {

                    @Override
                    public void onRequestEnd() {
                        super.onRequestEnd();
                        if (hud != null && hud.isShowing()) {
                            hud.dismiss();
                        }
                    }
                    @Override
                    public void onHandleSuccess(ApplyInfoBean info) {
                        //处理得到的数据
                        resultData(info);
                    }

                    @Override
                    public void onHandleError(int code, String message) {

                    }
                });
    }

    private void resultData(ApplyInfoBean info){
        servicename.setText("退款");
        money.setText("￥" + info.getPayMoney());
        amountMoney.setText("(含邮费￥" + info.getTransferFee() + ")");
        groutbei.setText("￥" + info.getAmountRebate());
        outpayBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitApply(orderid);
            }
        });
    }


    private void submitApply(String oid){
        String userid = PreferencesUtils.getStringPreference(this, OrderPreperActivity.prefreceName, OrderPreperActivity.prefrece_userid, ""); //获取用户id没有登陆就是没有

        Map<String, String> params = new HashMap<>();
        params.put("orderId", oid);
        params.put("userid", userid);
        ModelFilteredFactory.compose(RetrofitFactory.getInstance().API().SUBMITAPPLY(params))
                .compose(this.<BaseParse<String>>bindUntilEvent(ActivityEvent.DESTROY)) //跟生命周期绑定一起
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        //开始监听前的处理，一般用于显示dialog什么的
////                        if (hud != null) {
////                            hud.show();
////                        }
                        if (hud != null && !hud.isShowing()) {
                            hud.show();
                        }
                        Log.d("accept", "accept");
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>(this) {

                    @Override
                    public void onRequestEnd() {
                        super.onRequestEnd();
                        if (hud != null && hud.isShowing()) {
                            hud.dismiss();
                        }
                    }
                    @Override
                    public void onHandleSuccess(String info) {
                        //成功后的跳转
                        Intent it = new Intent(ApplyRefund.this,ActivityApplyResultInfo.class);
                        it.putExtra(ActivityApplyResultInfo.EXTRA_ORDERID,orderid);
                        startActivity(it);
                        ApplyRefund.this.finish();
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        //失败会显示
                        if(null != message)
                            Toaster.showOneToast(message);
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        super.onFailure(e);

                    }
                });
    }
}
